<template>
  <el-dialog :title="$t('reviewRiskList.manageModule.riskSetting')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <!-- 是否开启 -->
      <el-form-item :label="$t('reviewRiskList.manageModule.isopen')" prop="activate">
        <el-select v-model="formValidate.activate" clearable :placeholder="$t('common.select')">
          <el-option
            v-for="item in $t('reviewRiskList.manageModule.statusType')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <!-- 是否必填 -->
      <el-form-item v-if="formValidate.id === 5" :label="$t('reviewRiskList.manageModule.required')" prop="status">
        <el-select v-model="formValidate.status" clearable :placeholder="$t('common.select')">
          <el-option
            v-for="item in $t('reviewRiskList.manageModule.requiredType')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <!-- 信审订单有效天数 -->
      <el-form-item v-if="formValidate.id === 8" :label="$t('reviewRiskList.manageModule.expirationDate')" prop="exemptionDays">
        <el-row>
          <el-col :span="16">
            <el-input-number v-model="formValidate.exemptionDays" controls-position="right" :min="0" :max="100000" style="width: 100%" />
          </el-col>
          <el-col :span="6" :offset="1">
            {{ $t('reviewRiskList.manageModule.day') }}
          </el-col>
        </el-row>
      </el-form-item>
      <!-- 免检天数 -->
      <el-form-item v-if="formValidate.id === 6" :label="$t('reviewRiskList.manageModule.uncheck')" prop="exemptionDays">
        <el-row>
          <el-col :span="16">
            <el-input-number v-model="formValidate.exemptionDays" controls-position="right" :min="0" :max="1000000" style="width: 100%" />
          </el-col>
          <el-col :span="6" :offset="1">
            {{ $t('reviewRiskList.manageModule.day') }}
          </el-col>
        </el-row>
      </el-form-item>
      <!-- 活体识别 -->
      <el-form-item v-if="formValidate.id === 7" :label="$t('reviewRiskList.manageModule.face')" prop="exemptionDays">
        <el-select v-model="formValidate.exemptionDays" clearable :placeholder="$t('common.select')">
          <el-option
            v-for="item in $t('reviewRiskList.manageModule.faceType')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { setBusinessParam } from '@/api/risk'
export default {
  name: 'RiskSettingDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    const validateExemptionDays = (rule, value, callback) => {
      if (!value || value.length === 0) {
        if (this.formValidate.id === 8) {
          callback(new Error(this.$t('reviewRiskList.manageModule.expirationDate') + this.$t('common.null')))
        }
        if (this.formValidate.id === 6) {
          callback(new Error(this.$t('reviewRiskList.manageModule.uncheck') + this.$t('common.null')))
        }
        if (this.formValidate.id === 7) {
          callback(new Error(this.$t('reviewRiskList.manageModule.face') + this.$t('common.null')))
        }
      } else {
        callback()
      }
    }
    return {
      file: null,
      submitLoading: false,
      formValidate: {
        id: null,
        businessId: null,
        exemptionDays: null,
        status: null,
        activate: null
      },
      rules: {
        exemptionDays: [{ required: true, type: 'number', message: this.$t('systemModule.admin.table.password') + this.$t('common.null'), trigger: 'blur' }],
        status: [{ required: true, type: 'number', validator: validateExemptionDays, trigger: 'blur' }],
        activate: [{ required: true, type: 'number', message: this.$t('reviewRiskList.manageModule.isopen') + this.$t('common.null'), trigger: 'blur' }]
      },
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          // this.submitLoading = true
          const data = this.formValidate
          console.log(data)
          const postObj = {
            businessId: data['businessId'],
            status: data['status'],
            exemptionDays: data['exemptionDays'] || 0,
            activate: data['activate'],
            id: data['id']
          }
          setBusinessParam(postObj)
            .then(res => {
              this.submitLoading = false
              if (res.success) {
                this.$message.success(this.$t('common.tip.editSuccess'))
                this.$emit('edit-success')
                this.handleClose()
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(err => {
              this.$message.error(err.message)
              this.submitLoading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .admin-tree{
    margin-top: 20px;
  }
</style>
